package com.kangyi.user.controller;

import com.kangyi.dto.ApplicationDTO;
import com.kangyi.result.Result;
import com.kangyi.user.service.GuestsStayingService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.GuestsStayingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController("userGuestsStayingController")
@RequestMapping("/user/guests")
@Slf4j
@Api(tags = "入住人相关接口")
public class GuestsStayingController {

    @Autowired
    private GuestsStayingService guestsStayingService;

    /**
     * 根据用户id查询账号下的入住人
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    @ApiOperation("根据用户id查询账号下的入住人")
    @Cacheable(cacheNames = "guestsStaying_",key = "'userQuery_'+#id")
    public Result<List<GuestsStayingVO>> queryGuestsStaying(@PathVariable Integer id){
        log.info("根据用户id查询账号下的入住人:{}",id);
        try {
            List<GuestsStayingVO> guestsStayingVOS = guestsStayingService.queryGuestsStaying(id);
            return Result.success(guestsStayingVOS,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/inquiry/{guestId}")
    @ApiOperation("查询入住人信息")
    @Cacheable(cacheNames = "guestsStaying_",key = "'userInquiry_'+#guestId")
    public Result<GuestsStayingVO> inquiryGuestsStaying(@PathVariable Integer guestId){
        log.info("查询入住人信息：{}",guestId);
        try {
            GuestsStayingVO guestsStayingVO = guestsStayingService.inquiryGuestsStaying(guestId);
            return Result.success(guestsStayingVO,"查询成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 用户提交入住申请
     */
    @PostMapping("/apply")
    @ApiOperation("用户提交入住申请")
    @CacheEvict(cacheNames = "guestsStaying_", allEntries = true)
    public Result apply(@RequestBody ApplicationDTO applicationDTO) {
        log.info("用户提交入住申请:{}",applicationDTO);
        try {
            AuthUtil.validateUserPermission(applicationDTO.getUserId());
            guestsStayingService.applyForNursingHome(applicationDTO);
            return Result.success("申请提交成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
